package com.ruoyi.system.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.system.domain.SysPythonAddressEntity;
import com.ruoyi.system.domain.SysPythonEntity;
import com.ruoyi.system.domain.enums.BilbiliRidEnum;
import com.ruoyi.system.mapper.ISysPythonAddressMapper;
import com.ruoyi.system.mapper.ISysPythonMapper;
import com.ruoyi.system.util.UrlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 哔哩哔哩计时器
 */
@Component
public class PythonTask {
    @Autowired
    private ISysPythonMapper iSysPythonMapper;
    @Autowired
    private ApplicationContext applicationContext;
    @Autowired
    private ISysPythonAddressMapper iSysPythonAddressMapper;
    @Autowired
    private RedisCache redisCache;
    private   boolean statusRedis = false;
    //bilibili定时
    @Scheduled(cron = "0/2 * * * * ?")
    public void biliGames(){
        try {
            sysPython(BilbiliRidEnum.URL.getName() + BilbiliRidEnum.GAME.getNumber());
            sysPython(BilbiliRidEnum.URL.getName() + "188");
            //知识区
            sysPython(BilbiliRidEnum.URL.getName() + BilbiliRidEnum.LEARN.getNumber());
            Long ll = redisCache.getExpire("python::");
            if (redisCache.getExpire("python::") == -2&&!statusRedis) {
                statusRedis = true;
                redisCache.setCacheObject("python::", "value", 300, TimeUnit.SECONDS);
            }
            if (redisCache.getExpire("python::") == -1&&!statusRedis) {
                statusRedis = true;
                redisCache.expire("python::", 300);
            }
        }catch (Exception e){
            System.out.println(e);
        }finally {
            if (redisCache.getExpire("python::") <= 0&&statusRedis) {
//             获取线程池实例并关闭线程池
                ThreadPoolTaskExecutor executorService = applicationContext.getBean("handleMsgExecutor", ThreadPoolTaskExecutor.class);
                executorService.shutdown();
                // 初始化
                executorService.initialize();
                statusRedis = false;
            }
        }


    }

//    @Async("handleMsgExecutor")
//    @Transactional
    @Scheduled(cron = "0/3 * * * * ?")
    public void deleteRepeat() {
        //定时删除重复行
        Integer num = iSysPythonMapper.deleteRepeat();
        if (num>0){
            deleteRepeat();
        }
    }
    @Scheduled(cron = "0/50 * * * * ?")
    public void addressNameRedis(){
        List<SysPythonAddressEntity> sysPythonAddressEntities =  iSysPythonAddressMapper.selectList(new LambdaQueryWrapper<>());
        if (redisCache.getExpire("addressName::")==-2){
            redisCache.setCacheList("addressName::",sysPythonAddressEntities);
        }

    }
    @Async("handleMsgExecutor")
    public void sysPython(String url){
        if (UrlUtil.crawlRecommendationRoomses(url)==null||UrlUtil.crawlRecommendationRoomses(url).getData() == null){
            return;
        }
        List<Map<String,Object>> listdata = UrlUtil.crawlRecommendationRoomses(url).getData();
        if (listdata ==null){
            return;
        }
        listdata.forEach(data -> {
            SysPythonEntity entityData = new SysPythonEntity();

            if (url.contains("bilibili")) {
                entityData.setTitle(data.get("title").toString());
                entityData.setAddress(BilbiliRidEnum.URL.getNumber());
                entityData.setTName(((Map<Object, Object>) data.get("owner")).get("name").toString());
                entityData.setUrl(data.get("short_link_v2").toString());
                entityData.setValue(data.get("desc").toString());
                entityData.setSubject(data.get("tname").toString());
                entityData.setNum((Integer) ((Map<Object, Object>) data.get("stat")).get("vv"));
                //弹幕
                entityData.setDuration((Integer) ((Map<Object, Object>) data.get("stat")).get("danmaku"));
                entityData.setCreatTime(LocalDateTime.now());
                entityData.setUpdateTime(LocalDateTime.now());
            }
            iSysPythonMapper.insertData(entityData);


        });


    }
}
